<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="72px" class="queryForms">
			<el-form-item label="设备名称" prop="deviceName">
				<el-input v-model="queryParams.deviceName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="保养状态" prop="maintenanceState">
				<el-select v-model="queryParams.maintenanceState" placeholder="请选择">
					<el-option v-for="dict in dict.type.maintain_state" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="设备编号" prop="deviceNumber">
				<el-input v-model="queryParams.deviceNumber" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="设备类型" prop="deviceTypeName">
				<el-input v-model="queryParams.deviceTypeName" placeholder="请输入设备类型" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="保养时间" prop="month">
				<el-input v-model="queryParams.month" type="number" placeholder="请输入数字" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit  />
			</el-form-item>
			<el-form-item label="保养人" prop="maintenanceUser">
				<el-input v-model="queryParams.maintenanceUser" placeholder="请输入保养人" clearable @keyup.enter.native="handleQuery" maxlength="10" show-word-limit />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['plan:maintainTask:export']">导出保养记录</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="maintainTask24" :showSearch.sync="showSearch" @queryTable="handleQuery" :columns.sync="columns" perm="plan:maintainTask:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="TDeviceMaintenanceTaskList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="设备名称" prop="deviceName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="设备型号" prop="deviceModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="设备编号" prop="deviceNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<el-table-column label="设备类型" prop="deviceTypeName" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<el-table-column label="保养时间" align="center" prop="month" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible">
					<template slot-scope="scope"> {{ scope.row.month }}月 </template>
				</el-table-column>
				<el-table-column label="保养级别" align="center" prop="maintenanceLevel" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="保养完成时间" align="center" prop="maintenanceTime" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.maintenanceTime, "{y}-{m}-{d}") }}</span>
					</template>
				</el-table-column>
				<el-table-column label="保养状态" prop="maintenanceStateText" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[7].visible" />
				<el-table-column label="保养人" prop="maintenanceUser" min-width="150" :show-overflow-tooltip="true" align="center" v-if="columns[8].visible" />
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="210">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleComplete(scope.row)" v-if="scope.row.maintenanceState == '0' || scope.row.maintenanceState == '2'" v-hasPermi="['plan:maintainTask:complete']">完成</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleAnnex(scope.row)" v-hasPermi="['plan:maintainTask:annex']">附件</el-button>
						<el-button size="mini" type="text" icon="el-icon-s-order" @click="handleContent(scope.row)" v-hasPermi="['plan:maintainTask:content']">保养内容</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination28" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<el-dialog title="保养内容" :visible.sync="contopen" width="900px" append-to-body v-dialogDrag>
			<div class="maintain-content">
				<p v-for="(item, index) in contlist" :key="index">
					{{ item.maintenanceContent }}
				</p>
				<p v-if="contlist.length == 0" style="text-align: center">暂无数据</p>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="contcancel">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listTDeviceMaintenanceTask, getTDeviceMaintenanceTask, delTDeviceMaintenanceTask, addTDeviceMaintenanceTask, updateTDeviceMaintenanceTask, completeTask, getMaintainContent } from "@/api/plan/maintainTask"
import { mapGetters } from "vuex"
import DocumentShow from "@/views/components/DocumentShow"
import { getcolumn } from "@/api/selectList"
export default {
	name: "MaintainTask",
	dicts: ["maintain_state"],
	computed: {
		...mapGetters(["sidebar"])
	},
	components: { DocumentShow },
	data() {
		return {
			fileVedioList: [],
			showfileListDialog: false,
			showTodoListDialog: false,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 保养任务表格数据
			TDeviceMaintenanceTaskList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				deviceName: undefined,
				maintenanceState: undefined,
				deviceNumber: undefined,
				deviceTypeName: undefined,
				month: undefined,
				maintenanceUser: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
			},
			contopen: false,
			contlist: [],
			// 列信息
			columns: [
				{ key: 0, label: `设备名称`, visible: true },
				{ key: 1, label: `设备型号`, visible: true },
				{ key: 2, label: `设备编号`, visible: true },
				{ key: 3, label: `设备类型`, visible: true },
				{ key: 4, label: `保养时间`, visible: true },
				{ key: 5, label: `保养级别`, visible: true },
				{ key: 6, label: `保养完成时间`, visible: true },
				{ key: 7, label: `保养状态`, visible: true },
				{ key: 8, label: `保养人`, visible: true }
			]
		}
	},
	created() {
		this.getSize()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination28" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询保养任务列表 */
		getList() {
			this.loading = true
			listTDeviceMaintenanceTask(this.queryParams).then(response => {
				this.TDeviceMaintenanceTaskList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: undefined,
				maintenanceScheduleDevicePlanId: undefined,
				deviceId: undefined,
				deviceName: undefined,
				deviceModel: undefined,
				deviceNumber: undefined,
				deviceTypeName: undefined,
				month: undefined,
				maintenanceLevel: undefined,
				maintenanceTime: undefined,
				maintenanceUserId: undefined,
				maintenanceUser: undefined,
				maintenanceState: undefined,
				remark: undefined,
				delFlag: undefined,
				createBy: undefined,
				createTime: undefined,
				updateBy: undefined,
				updateTime: undefined
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加保养任务"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getTDeviceMaintenanceTask(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改保养任务"
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.id != undefined) {
						updateTDeviceMaintenanceTask(this.form).then(response => {
							this.$modal.msgSuccess("修改成功")
							this.open = false
							this.getList()
						})
					} else {
						addTDeviceMaintenanceTask(this.form).then(response => {
							this.$modal.msgSuccess("新增成功")
							this.open = false
							this.getList()
						})
					}
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm('是否确认删除保养任务编号为"' + ids + '"的数据项？')
				.then(function () {
					return delTDeviceMaintenanceTask(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"TDeviceMaintenanceTask/TDeviceMaintenanceTask/export",
				{
					...this.queryParams
				},
				`TDeviceMaintenanceTask_${new Date().getTime()}.xlsx`
			)
		},

		// 完成
		handleComplete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认完成数据项？")
				.then(function () {
					return completeTask(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("操作成功")
				})
				.catch(() => {})
		},
		// 保养内容
		async handleContent(row) {
			let ret = await getMaintainContent(row.id)
			this.contlist = ret.rows
			if (this.contlist.length == 0) {
				this.$modal.msg("暂无数据")
			} else {
				this.contopen = true
			}
		},
		contcancel() {
			this.contopen = false
		},
		// 附件
		handleAnnex(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 10,
					perm: "plan:maintainTask:annex"
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped>
.maintain-content {
	p {
		line-height: 34px;
		padding: 0 10px;
	}
}
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 210px !important;
}
</style>
